package com.t2b.thaicalendar.view;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.t2b.thaicalendar.R;
import com.t2b.thaicalendar.dto.Days;
import com.t2b.thaicalendar.dto.DaysOfWeekDto;

/**
 * Created by j1tth4 on 6/29/14.
 */
public class WeekView extends LinearLayout {

    private LayoutInflater mInflater;

    public WeekView(Context context, DaysOfWeekDto daysOfWeekDto) {
        super(context);
        mInflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//        setDividerDrawable(getResources().getDrawable(android.R.drawable.divider_horizontal_bright));
//        setShowDividers(LinearLayout.SHOW_DIVIDER_MIDDLE | LinearLayout.SHOW_DIVIDER_END);
//        setDividerPadding(4);
        drawDays(daysOfWeekDto);
    }

    protected void drawDays(DaysOfWeekDto daysOfWeekDto){
        for(int i = 0; i < daysOfWeekDto.getMaximumDaysOfWeek(); i++){
            RelativeLayout dayContent = (RelativeLayout) mInflater.inflate(R.layout.day, null);
            TextView tvDaysNum = (TextView) dayContent.findViewById(R.id.tvDay);
            final Days days = daysOfWeekDto.daysOfWeek.get(i);
            dayContent.setOnClickListener(new OnDayClickListener());
            if ((Boolean) days.data.get(Days.TO_MONTH)) { // in month
                tvDaysNum.setText(days.data.get(Days.DAY).toString());
                if ((Boolean)days.data.get(Days.TO_DAY)) { // to day
                    dayContent.setSelected(true);
//                    ImageView imgToday = new ImageView(getContext());
//                    imgToday.setImageResource(R.drawable.ic_stat_today);
//                    RelativeLayout.LayoutParams params = getParams();
//                    params.addRule(RelativeLayout.CENTER_HORIZONTAL);
//                    params.addRule(RelativeLayout.CENTER_VERTICAL);
//                    dayContent.addView(imgToday, params);
                }else if (i == 0) { //in sunday
                    tvDaysNum.setTextColor(getResources().getColor(R.color.red));
                }
                /***********Buddha Moon Phase Day***************/
                if(days.data.get(Days.WAXING) != null) {
                    //                      TextView tvBuddha = new TextView(getContext());
                    //                    tvBuddha.setText(
                    //                            ((days.data.get(Days.WAXING).equals("WX") ?
                    //                                    getContext().getString(R.string.buddhaWaxing) :
                    //                                    getContext().getString(R.string.buddhaWaning)) + " "
                    //                                    + days.data.get(Days.WAXING_DAY) + " " +
                    //                                    getContext().getString(R.string.buddhaDayPrefix)) + " "
                    //                                    + getContext().getString(R.string.buddhaMonthPrefix) + " "
                    //                                    + days.data.get(Days.WAXING_MONTH_2)
                    //                    );
                    //                    tvBuddha.setTextColor(getResources().getColor(R.color.black));
                    //WanPhra
                    if ((Boolean)days.data.get(Days.WAXING_WANPRA)) {
                        ImageView imgBuddha = new ImageView(getContext());
                        imgBuddha.setImageResource(R.drawable.ic_wanpra);
                        RelativeLayout.LayoutParams params = getParams();
                        //params.width = 32;
                        //params.height = 32;
                        params.addRule(RelativeLayout.ALIGN_PARENT_TOP);
                        params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
                        dayContent.addView(imgBuddha, params);
                    }
                    if (i == 0) { //in sunday
                        tvDaysNum.setTextColor(getResources().getColor(R.color.red));
                    }
                }
                /*****************Holiday*************************/
                if(days.data.get(Days.HOLIDAY) != null){
                    tvDaysNum.setTextColor(Color.RED);
                    //tvHoliday.setVisibility(View.VISIBLE);
                    //tvDaysNum.setTextColor(getResources().getColor(R.color.red));
                    //tvHoliday.setText(days.data.get(Days.HOLIDAY).toString());
                }
            }
            addView(dayContent, Utils.getHorizonParams());
        }
    }
    private class OnDayClickListener implements OnClickListener {

        @Override
        public void onClick(View view) {
            new AlertDialog.Builder(getContext())
                    .setTitle("Event")
                    .setMessage("Event Detail")
                    .setNegativeButton("Close", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {

                        }
                    }).show();
        }
    }

    public static RelativeLayout.LayoutParams getParams(){
        return new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);
    }
}